import java.io.*;
import java.util.Objects;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {

        String path = "D:\\code\\cvis-ui";
        File file = new File(path);
        String oldName = "aids";
        String oldName2 = "Aids";
        String oldName3 = "AIDS";

        String newName = "cvis";
        String newName2 = "Cvis";
        String newName3 = "CVIS";
        // 修改文件夹名
        renameFolder(file, oldName, newName);
        // 修改文件名
        traverseFolderEditFileName(file, oldName2, newName2);
        // 修改文件内容
        traverseFolderReplaceLine(file, oldName, newName);
        // 修改文件内容2
        traverseFolderReplaceLine(file, oldName2, newName2);
        // 修改文件内容3
        traverseFolderReplaceLine(file, oldName3, newName3);
    }

    public static void main1(String[] args) {
        String path = "D:\\workspace\\aids-ui";
        File file = new File(path);
        String name = "首页";
        // 搜索文件
//        searchFileName(file, name);
        // 搜索内容
        searchFolderContent(file, name);
    }

    /**
     * 遍历文件修改文件名
     *
     * @param folder 文件
     * @param name 名字
     * @param newName 新名字
     */
    public static void traverseFolderEditFileName(File folder, String name, String newName) {
        Stack<File> stack = new Stack<>();
        stack.push(folder);

        while (!stack.isEmpty()) {
            File current = stack.pop();
            if (current.isDirectory()) {
                for (File file : Objects.requireNonNull(current.listFiles())) {
                    stack.push(file);
                }
            } else {
                String absolutePath = current.getAbsolutePath();
                editFileName(absolutePath, name, newName);
            }
        }
    }

    /**
     * 替换文件夹下所有文件行内容
     *
     * @param folder 文件夹
     * @param oldContent 旧内容
     * @param newContent 新内容
     */
    public static void traverseFolderReplaceLine(File folder, String oldContent, String newContent) {
        Stack<File> stack = new Stack<>();
        stack.push(folder);

        while (!stack.isEmpty()) {
            File current = stack.pop();
            if (current.isDirectory()) {
                for (File file : Objects.requireNonNull(current.listFiles())) {
                    stack.push(file);
                }
            } else {
                String absolutePath = current.getAbsolutePath();
                replaceContent(absolutePath, oldContent, newContent);
            }
        }
    }

    /**
     * 搜索文件夹下文件中的内容
     *
     * @param folder 文件夹
     * @param content 内容
     */
    public static void searchFolderContent(File folder, String content) {
        Stack<File> stack = new Stack<>();
        stack.push(folder);

        while (!stack.isEmpty()) {
            File current = stack.pop();
            if (current.isDirectory()) {
                for (File file : Objects.requireNonNull(current.listFiles())) {
                    stack.push(file);
                }
            } else {
                String absolutePath = current.getAbsolutePath();
                if (!absolutePath.contains("node_modules")) {
                    searchContent(absolutePath, content);
                }
            }
        }
    }

    /**
     * 搜索文件
     *
     * @param folder 文件夹
     * @param name 名称
     */
    public static void searchFileName(File folder, String name) {
        Stack<File> stack = new Stack<>();
        stack.push(folder);

        while (!stack.isEmpty()) {
            File current = stack.pop();
            if (current.isDirectory()) {
                for (File file : Objects.requireNonNull(current.listFiles())) {
                    stack.push(file);
                }
            } else {
                String absolutePath = current.getAbsolutePath();
                if (absolutePath.contains(name)) {
                    System.out.println(absolutePath);
                }
            }
        }
    }

    /**
     * 替换文件内容
     *
     * @param filePath 文件路径
     * @param searchContent 搜索内容
     * @param replaceContent 替换内容
     */
    public static void replaceContent(String filePath, String searchContent, String replaceContent){
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            int lineNumber = 0;
            while ((line = reader.readLine()) != null) {
                lineNumber++;
                if (line.contains(searchContent)) {
                    System.out.println("文件: " + filePath + ", 行数 " + lineNumber + ": " + line + "，新行内容：" + line.replace(searchContent, replaceContent));
                    replaceLine(filePath, lineNumber, line.replace(searchContent, replaceContent));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查找文件内容
     *
     * @param filePath 文件路径
     * @param searchContent 搜索内容
     */
    public static void searchContent(String filePath, String searchContent){
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            int lineNumber = 0;
            while ((line = reader.readLine()) != null) {
                lineNumber++;
                if (line.contains(searchContent)) {
                    System.out.println("文件: " + filePath + ", 行数 " + lineNumber + ": " + line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改文件名（模糊）
     *
     * @param absolutePath 文件路径
     * @param name 文件名
     * @param newName 新文件名
     */
    public static void editFileName(String absolutePath, String name, String newName){
        if(absolutePath.contains(name)){
            System.out.println("修改前文件：" + absolutePath);
            System.out.println("修改后文件：" + absolutePath.replace(name, newName));
            File oldFile = new File(absolutePath);
            // 创建File对象指向新的文件名
            File newFile = new File(absolutePath.replace(name, newName));
            // 使用renameTo方法修改文件名
            boolean renamed = oldFile.renameTo(newFile);
            // 输出结果
            if (renamed) {
                System.out.println("文件名已被成功修改。");
            } else {
                System.out.println("文件名修改失败。可能是文件不存在或者已经打开。");
            }
        }
    }

    /**
     * 替换文件行内容
     *
     * @param filePath 文件路径
     * @param lineNumber 行号
     * @param newContent 新行内容
     * @throws IOException 异常
     */
    public static void replaceLine(String filePath, int lineNumber, String newContent) throws IOException {
        // 打开文件，并将文件内容读取到内存中
        File file = new File(filePath);
        StringBuilder fileContent = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                fileContent.append(line).append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 查找需要替换的行，并将其修改为新的内容
        String[] lines = fileContent.toString().split(System.lineSeparator());
        if (lineNumber >= 1 && lineNumber <= lines.length) {
            lines[lineNumber - 1] = newContent;
        } else {
            System.out.println("Invalid line number.");
        }

        // 将修改后的内容写入文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (String line : lines) {
                writer.write(line);
                writer.write(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改文件名
     *
     * @param folder 文件
     * @param oldName 旧名字
     * @param newName 新文名字
     */
    public static void renameFolder(File folder, String oldName, String newName) {
        if (folder.exists() && folder.isDirectory()) {
            if (folder.getName().contains(oldName)) {
                File newFolder = new File(folder.getParent(), folder.getName().replace(oldName, newName));
                folder.renameTo(newFolder);
            }
            File[] listOfFiles = folder.listFiles();
            if (listOfFiles != null) {
                for (File file : listOfFiles) {
                    if (file.isDirectory()) {
                        renameFolder(file, oldName, newName);
                    }
                }
            }
        }
    }

}
